Completed
Pull Request — master (#24)
by Kyungmi
02:20
created

ui-router.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
c 5
b 0
f 0
nc 1
dl 0
loc 1
ccs 0
cts 1
cp 0
crap 2
rs 10
nop 1
1
/**
2
 * Service authenticator
3
 *
4
 * @since 1.0.0
5
 */
6
7 1
const StatusType = require('../repository/StatusType');
8 1
const DeviceType = require('../repository/DeviceType');
9
10 1
const menus = [
11
  { viewName: 'StatusList', title: '공지사항 관리', url: '/' },
12
  { viewName: 'Settings', title: '설정', url: '/settings' },
13
];
14
15
function view(request, reply, childViewName, context) {
16
  const ctx = context || {};
17
  ctx.viewName = childViewName;
18
  if (request.auth) {
19
    ctx.auth = {
20
      isAuthenticated: request.auth.isAuthenticated,
21
      username: request.auth.credentials ? request.auth.credentials.username : undefined,
22
    };
23
  }
24
  ctx.menus = menus;
25
  ctx.state = `window.state = ${JSON.stringify(ctx)}`;
26
  return reply.view('Layout', ctx);
27
}
28
29 1
module.exports = [
30
  {
31
    method: 'GET',
32
    path: '/',
33
    handler: (request, reply) => Promise.all([StatusType.find(), DeviceType.find()])
34
      .then(([statusTypes, deviceTypes]) => view(request, reply, 'StatusList', { statusTypes, deviceTypes }))
35
      .catch(error => reply(error)),
36
  },
37
  {
38
    method: 'GET',
39
    path: '/settings',
40
    handler: (request, reply) => Promise.all([
41
      StatusType.find(),
42
      DeviceType.find(),
43
    ]).then(([statusTypes, deviceTypes]) => view(request, reply, 'Settings', { statusTypes, deviceTypes }))
44
      .catch(error => reply(error)),
45
  },
46
  {
47
    method: 'GET',
48
    path: '/login',
49
    handler: (request, reply) => {
50
      if (request.auth.isAuthenticated) {
51
        return reply.redirect('/');
52
      }
53
      return view(request, reply, 'Login');
54
    },
55
    config: {
56
      auth: {
57
        mode: 'try',
58
      },
59
    },
60
  },
61
  {
62
    method: 'GET',
63
    path: '/logout',
64
    handler: (request, reply) => reply.redirect('/login').unstate('token'),
65
  },
66
  {
67
    method: 'GET',
68
    path: '/change-password',
69
    handler: (request, reply) => view(request, reply, 'ChangePassword'),
70
  },
71
];
72
73